package cn.kduck.code.entity.query;

import cn.kduck.core.dao.definition.BeanDefDepository;
import cn.kduck.core.dao.definition.BeanEntityDef;
import cn.kduck.core.dao.query.QueryCreator;
import cn.kduck.core.dao.query.QuerySupport;
import cn.kduck.core.dao.sqlbuilder.ConditionBuilder.ConditionType;
import cn.kduck.core.dao.sqlbuilder.SelectBuilder;
import cn.kduck.core.dao.sqlbuilder.SelectBuilder.AggregateType;
import cn.kduck.core.utils.BeanDefUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

import static cn.kduck.code.entity.service.EntityService.CODE_ENTITY_FIELD;

@Component
public class CountEntityFieldQuery implements QueryCreator {
    @Override
    public QuerySupport createQuery(Map<String, Object> paramMap, BeanDefDepository depository) {
        BeanEntityDef fieldDef = depository.getEntityDef(CODE_ENTITY_FIELD);
        SelectBuilder selectBuilder = new SelectBuilder(fieldDef,paramMap);
        selectBuilder.bindFields("", BeanDefUtils.includeField(fieldDef.getFieldList(),"fieldId"));
        selectBuilder.bindAggregate("FIELD_ID", AggregateType.COUNT);
        selectBuilder.where().and("ENTITY_ID", ConditionType.EQUALS,"entityId",true);
        return selectBuilder.build();
    }
}
